local _G = _G
local addon, ns = ...
local profile = ns.profile
local cfg = {}
local LabelUI
local noshow
local configframe
if not _G.LabelUI_mod then
	_G.LabelUI_mod = {}	
end
local Sharedmediamod = ns.Sharedmediamod
local Auroramod = ns.Auroramod
  
local Arkinventorymod = CreateFrame("Frame", nil, UIParent)


local function styleframe(id)
	Aurora[1].CreateBD(_G["ARKINV_Frame"..id])
	local scale = ArkInventory.LocationOptionGetReal( id, "window", "scale" )
	--Aurora[1].CreateSD(_G["ARKINV_Frame"..id])
	local kids = { _G["ARKINV_Frame"..id]:GetChildren() }
	for _, bg in ipairs(kids) do
		if bg and bg.GetBackdrop then
			local file = bg:GetBackdrop()
			if file then 
				file = file["edgeFile"]
				if file == Aurora[2].media.glow then 
					_G["ARKINV_Frame"..id].sd = bg
				end
			end
		end
	end
	if _G["ARKINV_Frame"..id].sd then
		_G["ARKINV_Frame"..id].sd:SetScale(1/scale)
	end
	--_G["ARKINV_Frame"..id]:HookScript("OnSizeChanged", function(self) 
	--	local scale = ArkInventory.LocationOptionGetReal( id, "window", "scale" )
	--	self.sd:SetScale(1/scale)
	--end)
	if _G["ARKINV_Frame"..id.."SearchFilter"] then
		Aurora[1].ReskinInput(_G["ARKINV_Frame"..id.."SearchFilter"])
	end
	if _G["ARKINV_Frame"..id.."TitleClose"] then
		Aurora[1].ReskinClose(_G["ARKINV_Frame"..id.."TitleClose"], "TOPRIGHT", _G["ARKINV_Frame"..id.."Title"], "TOPRIGHT", -8, -8)
		_G["ARKINV_Frame"..id.."TitleClose"]:SetScale(1.2)
	end
	if ArkInventory.LocationOptionGetReal( id, "window", "background", "colour", "a" ) ~= 0 then
		ArkInventory.LocationOptionSetReal( id, "window", "background", "colour", "a", 0 )
	end
	if ArkInventory.LocationOptionGetReal( id, "window", "border", "style" ) ~= "None" then
		ArkInventory.LocationOptionSetReal( id, "window", "border", "style", "None" )
	end	
	if ArkInventory.LocationOptionGetReal( id, "slot", "border", "style") ~= "ArkInventory Square 1" then
		ArkInventory.LocationOptionSetReal( id, "slot", "border", "style", "ArkInventory Square 1" )
									
		local sd = ArkInventory.Const.Texture.Border["ArkInventory Square 1"] or ArkInventory.Const.Texture.Border[ArkInventory.Const.Texture.BorderDefault]
		ArkInventory.LocationOptionSetReal( id, "slot", "border", "size", sd.size )
		ArkInventory.LocationOptionSetReal( id, "slot", "border", "offset", sd.offset )
		ArkInventory.LocationOptionSetReal( id, "slot", "border", "scale", sd.scale )
									
		ArkInventory.Frame_Main_Generate( nil, ArkInventory.Const.Window.Draw.Refresh )
	end
	if ArkInventory.LocationOptionGetReal( id, "bar", "border", "style" ) ~= "None" then								
		ArkInventory.LocationOptionSetReal( id, "bar", "border", "style", "None" )																	
	end
end
  
local function Enablemod()
	if Auroramod.ready then
		if Auroramod.additions["ArkinventorFrames"] then
			Auroramod.additions["ArkinventorFrames"] = nil
		end
		--Aurora[1].CreateBD(_G["ArkDewdrop30Level1"])
		local lastlevel = 0
		local hook
		hook = function()
			local frame = _G["ArkDewdrop30Level"..(lastlevel + 1)] 
			if frame and not frame.styled then
				local kids = { frame:GetChildren() }
				for _, bg in ipairs(kids) do
					if bg and bg.GetBackdrop then
						Aurora[1].CreateBD(bg)
					end
					break
				end
				lastlevel = lastlevel + 1
				frame.styled = true
				hook()
			end
		end
		hooksecurefunc(ArkInventory.Lib.Dewdrop, "AddLine", hook)
		local idbase = {1,3,4,5,6,7,8,9,10,11,13}
		if Sharedmediamod and Sharedmediamod.ready then
			ArkInventory.db.profile.option.font.name = "Aquarion"
			ArkInventory.MediaSetFontAll( )
		end
		for _,i in pairs(idbase) do
			styleframe(i)
		end
		ArkInventory.Frame_Main_Paint_All( )
	else
		Auroramod.additions["ArkinventorFrames"] = Enablemod	
	end	
end 
  
local function Init()
	_, _, _, enabled, _, reason = GetAddOnInfo("ArkInventory")
	if reason == "MISSING" or not enabled then return end
	if Auroramod then
		Enablemod()
	end
end

function Arkinventorymod.InitMod(main, profilename)
	noshow = main.noshow 
	LabelUI = main
	main:AddInit(Init, "Addon ArkinventoryMod")
end 
  
_G.LabelUI_mod["Arkinventorymod"] = Arkinventorymod